﻿using System;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using ASPNETVN.PORTAL.Components.Configuration;
using System.Data;
using System.Linq;
using ASPNETVN.PORTAL.Modules.DOCUMENT.Components;
using System.Reflection;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.HOME.Controls
{
    public partial class Style4 : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Hashtable settings = this.Settings;
                int recordNumber = Convert.ToInt32(settings["RecordNumber"].ToString());
                Guid moduleID = new Guid(settings["DOCUMENT.ModuleID"].ToString());

                DataTable dt = Components.Item.SelectBy_ModuleID(moduleID).Tables[0];

                var query1 = (from r in dt.AsEnumerable()
                             select new { Code = r.Field<string>("Code"), PublishedDate = r.Field<DateTime>("PublishedDate"), Summary = r.Field<string>("Summary") }).OrderByDescending(x => x.PublishedDate).Distinct().Take(recordNumber);

                var query2 = (from r in dt.AsEnumerable()
                              select new { Code = r.Field<string>("Code"), CreatedDate = r.Field<DateTime>("CreatedDate"), PublishedDate = r.Field<DateTime>("PublishedDate"), Summary = r.Field<string>("Summary") }).OrderByDescending(x => x.CreatedDate).Select(x => new { Code = x.Code, PublishedDate = x.PublishedDate, Summary = x.Summary }).Distinct().Take(recordNumber);

                gvItem1.DataSource = query1;
                gvItem1.DataBind();

                gvItem2.DataSource = query2;
                gvItem2.DataBind();
            }
        }

        protected string GetUrl(string code)
        {
            int itemID = Item.GetItemID_ByCode(code);
            Guid tabID = Guid.Empty;
            PortalDataSet ds = PortalConfiguration.GetPortalDataSet();
            Hashtable settings = this.Settings;
            Guid moduleID = new Guid(settings["DOCUMENT.ModuleID"].ToString());

            foreach (PortalDataSet.TabRow tab in ds.Tab.Rows)
            {
                foreach (PortalDataSet.ModuleRow module in tab.GetModuleRows())
                {
                    if (module.ID == new Guid(this.Settings["DOCUMENT.ModuleID"].ToString()))
                    {
                        return string.Format("~/Default.aspx?tabid={0}&mid={1}&itemid={2}&page=detail", tab.ID, moduleID, itemID);
                    }
                }
            }
            return string.Format("~/Default.aspx");
        }
    }
}